package com.dcy.file.biz.config.file;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.dcy.file.biz.model.FileInfo;
import com.dcy.file.biz.properties.FileServerProperties;
import com.dcy.file.biz.service.IFileHandler;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadFileStream;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author：dcy
 * @Description: FastDFS配置
 * @Date: 2019/9/18 13:59
 */
@Configuration
@ConditionalOnProperty(name = "dcy.file-server.type", havingValue = "fastdfs")
public class FastdfsAutoConfigure {

    @Autowired
    private FileServerProperties fileProperties;
    @Autowired
    private FdfsWebServer fdfsWebServer;


    @Service
    public class FastdfsServiceImpl implements IFileHandler {
        @Autowired
        private FastFileStorageClient storageClient;

        @Override
        public String fileType() {
            return fileProperties.getType();
        }

        /**
         * 上传文件
         *
         * @param file
         * @param fileInfo
         * @throws Exception
         */
        @Override
        public void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception {
            this.uploadFile(file.getInputStream(), fileInfo);
        }

        @Override
        public void uploadFile(InputStream inputStream, FileInfo fileInfo) throws Exception {
            StorePath storePath = storageClient.uploadFile(inputStream, inputStream.available(), FilenameUtils.getExtension(fileInfo.getName()), null);
            fileInfo.setUrl("http://" + fdfsWebServer.getWebServerUrl() + "/" + storePath.getFullPath());
            fileInfo.setPath(storePath.getFullPath());
        }

        /**
         * 删除文件
         *
         * @param fileInfo
         * @return
         */
        @Override
        public boolean deleteFile(FileInfo fileInfo) {
            if (fileInfo != null && StrUtil.isNotBlank(fileInfo.getPath())) {
                StorePath storePath = StorePath.parseFromUrl(fileInfo.getPath());
                storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
            }
            return true;
        }

        /**
         * 下载文件
         *
         * @param fileName
         * @param filePath
         * @param response
         */
        @Override
        public void downLoadFile(String fileName, String filePath, HttpServletResponse response) {
            StorePath storePath = StorePath.parseFromUrl(filePath);
            try {
                ServletOutputStream outputStream = response.getOutputStream();
                DownloadFileStream downloadFileStream = new DownloadFileStream(outputStream);
                storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), downloadFileStream);
                IoUtil.close(outputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
